package com.svw.asmpcloud.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.svw.asmpcloud.bean.UriSimpleGrantedAuthority;
import com.svw.asmpcloud.bean.User;
import com.svw.asmpcloud.service.UserService;

@Service
public class UserServiceImpl implements UserService {

    private static Map<String,User> users = new HashMap<String,User>();

    static {
            // 用户1
            User user1 = new User();
            user1.setUsername("aaa");
            user1.setPassword("111");
            List<UriSimpleGrantedAuthority> uris1 = new ArrayList<UriSimpleGrantedAuthority>();
            uris1.add(new UriSimpleGrantedAuthority("/auth-central-try-producer/user1Call"));
            uris1.add(new UriSimpleGrantedAuthority("/auth-central-try-producer/user2Call"));
            user1.setUris(uris1);
            users.put("aaa",user1);
            // 用户2
            User user2 = new User();
            user2.setUsername("bbb");
            user2.setPassword("111");
            List<UriSimpleGrantedAuthority> uris2 = new ArrayList<UriSimpleGrantedAuthority>();
            uris2.add(new UriSimpleGrantedAuthority("/auth-central-try-producer/user1Call"));
            user2.setUris(uris2);
            users.put("bbb",user2);
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 首先从redis中去读,如果没有再从数据库中去读
        User user = users.get(username);
        if (user == null) {
            throw new UsernameNotFoundException("用户不存在或者密码错误");
        }
        return user;
    }

}
